package com.dg11185.nearshop.activity.shop;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.nearshop.R;
import com.dg11185.libs.imageloader.core.ImageLoader;
import com.dg11185.libs.utils.screen.ScreenSize;
import com.dg11185.nearshop.app.DefaultFactory;
import com.dg11185.nearshop.net.NetCacheClient;
import com.dg11185.nearshop.net.entity.Photo;
import com.dg11185.nearshop.net.request.GetMerchantPhotoListRequest;
import com.dg11185.nearshop.net.response.GetMerchantPhotoListResponse;
import com.dg11185.nearshop.net.response.NetStatus;
import com.nineoldandroids.animation.ObjectAnimator;
import com.wlper.lib.uitls.ViewUtils;

public class AlbumHelper {

    private Activity context = null;

    private ViewPager viewPager;

    private List<Photo> albums = new ArrayList<Photo>();

    private AlbumPagerAdapter adapter;

    private int curPageNo = 1, total = -1, pageSize = 10;

    private GetMerchantPhotoListRequest request;

    private boolean isShopAblumShow = false;
    public AlbumHelper(Activity context, String merchantId) {
        this.context = context;
        viewPager = (ViewPager) context.findViewById(R.id.shop_detail_album_pager);
        viewPager.setOnPageChangeListener(new PageChangeListener());
        viewPager.setOnClickListener(new ClickListener());
        adapter = new AlbumPagerAdapter();
        viewPager.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        request = new GetMerchantPhotoListRequest(merchantId).add("pageSize", pageSize + "");
        getPhotoList();
    }

    public void showShopAlbum() {
        
        if (total == 0) {
            ViewUtils.showToast(context, "该商家还没有创建相册");
        }else {
            isShopAblumShow = true;
            viewPager.setVisibility(View.VISIBLE);
            ObjectAnimator.ofFloat(viewPager, "translationY", viewPager.getHeight(), 0).setDuration(200).start();
        }
    }

    public boolean albumShow(){
        return isShopAblumShow;
    }
    
    public void hideShopAlbum(){
        isShopAblumShow = false;
        ObjectAnimator.ofFloat(viewPager, "translationY", 0, viewPager.getHeight()).setDuration(200).start();
    }
    public class ClickListener implements OnClickListener {
        @Override
        public void onClick(View v) {
            hideShopAlbum();
        }

    }

    private void getPhotoList() {
        request.getParams().remove("chshSign");
        request.add("pageNumber", curPageNo + "").encryption();
        request.setActionListener(new GetMerchantPhotoListRequest.ActionListener<GetMerchantPhotoListResponse>() {
            @Override
            public void onSuccess(GetMerchantPhotoListResponse result) {
                if (result.status == NetStatus.SUCCESS) {
                    total = Integer.parseInt(result.total);
                    if (result.photos.size() != 0) {
                        albums.addAll(result.photos);
                        adapter.setTotal(total);
                        adapter.notifyDataSetChanged();
                    }else{
                        
                    }
                } else {
                    onFailure(-1);
                }
            }

            @Override
            public void onFailure(int resultCode) {

            }
        });
        NetCacheClient.httpCacheGet(request);
    }

    private class PageChangeListener implements OnPageChangeListener {
        @Override
        public void onPageScrollStateChanged(int arg0) {

        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {

        }

        @Override
        public void onPageSelected(int arg0) {
            if (curPageNo * pageSize < total) {
                if (arg0 == (curPageNo * pageSize - 1)) {
                    curPageNo++;
                    getPhotoList();
                }
            }
        }
    }

    public class MyGestureListener extends SimpleOnGestureListener {

        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            System.out.println(e1.getY() + "/" + e2.getY());
            return false;
        }

    }

    private class AlbumPagerAdapter extends PagerAdapter {

        private RelativeLayout.LayoutParams layoutParams;

        private int total = 0;

        public AlbumPagerAdapter() {
            layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
            layoutParams.width = ScreenSize.getInstance().screenPxWidth;
            layoutParams.height = (int) (layoutParams.width * 0.62);
        }

        public void setTotal(int total) {
            this.total = total;
        }

        @Override
        public int getCount() {
            return albums.size();
        }

        @Override
        public Object instantiateItem(View container, int position) {
            
            View view = LayoutInflater.from(context).inflate(R.layout.shop_detail_album_item, null);
            ((ViewPager) container).addView(view);
            view.setOnClickListener(new ClickListener());
            ImageView photo = (ImageView) view.findViewById(R.id.album_photo);
            TextView content = (TextView) view.findViewById(R.id.album_content);
            TextView number = (TextView) view.findViewById(R.id.album_number);

            photo.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    
                }
            });
            String url = "";
            if (!"".equals(albums.get(position).maxPhotoUrl)) {
                url = albums.get(position).maxPhotoUrl;
            } else if (!"".equals(albums.get(position).midPhotoUrl)) {
                url = albums.get(position).midPhotoUrl;
            } else {
                url = albums.get(position).minPhotoUrl;
            }
            ImageLoader.getInstance().displayImage(url, photo, DefaultFactory.getImageoptions());
            // content.setText(albums.get(position).name);
            if (total != 0) {
                number.setText((position + 1) + "/" + total);
            } else {
                number.setText((position + 1) + "/" + getCount());
            }
            return view;
        }

        @Override
        public void destroyItem(View container, int position, Object object) {
            ((ViewPager) container).removeView((View) object);
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

    }
}
